import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import '../models/complaint.dart';

class DatabaseService {
  static final DatabaseService _instance = DatabaseService._internal();
  static Database? _database;

  factory DatabaseService() => _instance;

  DatabaseService._internal();

  Future<Database> get database async {
    if (_database != null) return _database!;
    _database = await _initDatabase();
    return _database!;
  }

  Future<Database> _initDatabase() async {
    String path = join(await getDatabasesPath(), 'eco_complaints.db');
    return await openDatabase(
      path,
      version: 1,
      onCreate: _onCreate,
    );
  }

  Future<void> _onCreate(Database db, int version) async {
    await db.execute('''
      CREATE TABLE complaints(
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        phone TEXT NOT NULL,
        type TEXT NOT NULL,
        description TEXT NOT NULL,
        imagePath TEXT,
        videoPath TEXT,
        latitude REAL NOT NULL,
        longitude REAL NOT NULL,
        address TEXT NOT NULL,
        createdAt TEXT NOT NULL,
        adminResponse TEXT,
        isProcessed INTEGER NOT NULL DEFAULT 0
      )
    ''');
  }

  Future<int> insertComplaint(Complaint complaint) async {
    final db = await database;
    return await db.insert('complaints', complaint.toMap());
  }

  Future<List<Complaint>> getComplaints() async {
    final db = await database;
    final List<Map<String, dynamic>> maps = await db.query('complaints');
    return List.generate(maps.length, (i) => Complaint.fromMap(maps[i]));
  }

  Future<Complaint?> getComplaint(int id) async {
    final db = await database;
    final List<Map<String, dynamic>> maps = await db.query(
      'complaints',
      where: 'id = ?',
      whereArgs: [id],
    );
    if (maps.isEmpty) return null;
    return Complaint.fromMap(maps.first);
  }

  Future<int> updateComplaint(Complaint complaint) async {
    final db = await database;
    return await db.update(
      'complaints',
      complaint.toMap(),
      where: 'id = ?',
      whereArgs: [complaint.id],
    );
  }

  Future<int> deleteComplaint(int id) async {
    final db = await database;
    return await db.delete(
      'complaints',
      where: 'id = ?',
      whereArgs: [id],
    );
  }

  Future<List<Complaint>> getAllComplaints() async {
    final db = await database;
    final List<Map<String, dynamic>> maps = await db.query('complaints', orderBy: 'createdAt DESC');
    return List.generate(maps.length, (i) => Complaint.fromMap(maps[i]));
  }

  Future<List<Complaint>> getComplaintsByPhone(String phone) async {
    final db = await database;
    final List<Map<String, dynamic>> maps = await db.query(
      'complaints',
      where: 'phone = ?',
      whereArgs: [phone],
      orderBy: 'createdAt DESC',
    );
    return List.generate(maps.length, (i) => Complaint.fromMap(maps[i]));
  }

  Future<void> processComplaint(int id, String response) async {
    final db = await database;
    await db.update(
      'complaints',
      {
        'adminResponse': response,
        'isProcessed': 1,
      },
      where: 'id = ?',
      whereArgs: [id],
    );
  }
} 